var baseURL = "/knowledge/";
$(function () {
    /*项目路径*/
    var baseURL = /knowledge/
    totalSize = 0;
    var currentSize = 0

    /*构建列表*/
    $("#jqGrid").jqGrid({
        url: baseURL + 'owlManage/getSemanticsList',
        datatype: "json",
        colModel: [
            {label: '语意', name: 'semanticsName', index: "semanticsCode", width: 45, key: true},
            {label: '关联关系', name: 'relationType', width: 75,formatter: function (value, options, row) {
	                if (value == 0) {
	                    return '近义词';
	                }
	                if (value == 1) {
	                    return '反义词';
	                }
	                if (value == 2) {
	                    return '父级类';
	                }
	                if (value == 3) {
	                    return '子级类';
	                }
            		}
            	},
            {label: '关联语意', name: 'relationSemanticsName', width: 90},
            {
                label: '操作', name: 'fileOwner', width: 80, formatter: function (value, options, row) {
                	var id = row.id;
                        return '<span class="label label-info"  onclick="deleteSem('+id+')">删除</span>';
                }
            },
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.pageNum",
            total: "page.pages",
            records: "page.total"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
})

var vm = new Vue({
    el: '#rrapp',
    data: {
        q: {
            key: null
        },
        showList: true,
        title: null,
        semantics: {
            documentUploadDate: null,
            documentAbstract: null
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:{'key': vm.q.key},
                page: page
            }).trigger("reloadGrid");
        },
        add: function () {
        	vm.showList = false;
            vm.title = "新增";
        }
    }
});

function deleteSem(id){
	$.post(baseURL + "/owlManage/delSemantics", {
    	"id":id
	}, function(result) {
		if(result.msg=="success"){
			alert("删除成功!");
			vm.reload();
		}else{
			alert("删除失败!");
			vm.reload();
		}
	})
	
}

$("#saveSemantics").click(function(){
	var semanticsName = vm.semantics.semanticsName;
	var relationType = vm.semantics.relationType;
	var relationSemanticsName = vm.semantics.relationSemanticsName;
	$.post(baseURL + "/owlManage/saveSemantics", {
    	"semanticsName":semanticsName,
    	"relationType":relationType,
    	"relationSemanticsName":relationSemanticsName
	}, function(result) {
		if(result.msg=="success"){
			alert("新增成功!");
			vm.reload();
		}else{
			alert("新增失败!");
			vm.reload();
		}
	})
})

$("#cancel").click(function(){
	vm.showList = true;
	vm.reload();
})